/*
 * 项目名称:platform-plus
 * 类名称:NcpCumulativeOfCasesController.java
 * 包名称:com.platform.modules.ncp.controller
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-02-11 18:03:21        tumin     初版做成
 *
 * Copyright (c) 2019-2019 微同软件
 */
package com.platform.modules.ncp.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.*;
import com.platform.modules.ncp.vo.NcpCumulativeOfCasesClinic;
import com.platform.modules.ncp.vo.NcpCumulativeOfCasesDiagnosis;
import com.platform.modules.ncp.vo.NcpCumulativeOfCasesSuspected;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.ncp.entity.NcpCumulativeOfCasesEntity;
import com.platform.modules.ncp.service.NcpCumulativeOfCasesService;
import com.platform.modules.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author tumin
 * @date 2020-02-11 18:03:21
 */
@RestController
@RequestMapping("ncp/cumulativeofcases")
public class NcpCumulativeOfCasesController extends AbstractController {
    @Autowired
    private NcpCumulativeOfCasesService ncpCumulativeOfCasesService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("ncp:cumulativeofcases:list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<NcpCumulativeOfCasesEntity> list = ncpCumulativeOfCasesService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 分页查询
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("ncp:cumulativeofcases:list")
    public RestResponse list(@RequestParam Map<String, Object> params) {
        Page page = ncpCumulativeOfCasesService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param persId 主键
     * @return RestResponse
     */
    @RequestMapping("/info/{persId}")
    @RequiresPermissions("ncp:cumulativeofcases:info")
    public RestResponse info(@PathVariable("persId") String persId) {
        NcpCumulativeOfCasesEntity ncpCumulativeOfCases = ncpCumulativeOfCasesService.getById(persId);

        return RestResponse.success().put("cumulativeofcases", ncpCumulativeOfCases);
    }

    /**
     * 新增
     *
     * @param ncpCumulativeOfCases ncpCumulativeOfCases
     * @return RestResponse
     */
    @SysLog("新增")
    @RequestMapping("/save")
    @RequiresPermissions("ncp:cumulativeofcases:save")
    public RestResponse save(@RequestBody NcpCumulativeOfCasesEntity ncpCumulativeOfCases) {

        ncpCumulativeOfCasesService.add(ncpCumulativeOfCases);

        return RestResponse.success();
    }

    @SysLog("导入疑似病例")
    @RequiresPermissions("ncp:cumulativeofcases:import")
    @PostMapping("/importSuspected")
    public RestResponse importSuspected(MultipartFile file,boolean updateSupport) throws Exception{
        List<NcpCumulativeOfCasesSuspected> ncpCumulativeOfCasesSuspecteds=ExcelUtil.importExcel(file,1,1, NcpCumulativeOfCasesSuspected.class);
        if(CollUtil.isEmpty(ncpCumulativeOfCasesSuspecteds))
            return RestResponse.error("导入失败！");
        List<NcpCumulativeOfCasesEntity> ncpCumulativeOfCasesEntitys= BeanCopier.copy(ncpCumulativeOfCasesSuspecteds,NcpCumulativeOfCasesEntity.class);
        return submitImport(ncpCumulativeOfCasesEntitys,1);
    }

    @SysLog("导入确诊病例")
    @RequiresPermissions("ncp:cumulativeofcases:import")
    @PostMapping("/importAccident")
    public RestResponse importAccident(MultipartFile file,boolean updateSupport) throws Exception{
        List<NcpCumulativeOfCasesDiagnosis> ncpCumulativeOfCasesDiagnosis=ExcelUtil.importExcel(file,1,1, NcpCumulativeOfCasesDiagnosis.class);
        if(CollUtil.isEmpty(ncpCumulativeOfCasesDiagnosis))
            return RestResponse.error("导入失败！");
        List<NcpCumulativeOfCasesEntity> ncpCumulativeOfCasesEntitys= BeanCopier.copy(ncpCumulativeOfCasesDiagnosis,NcpCumulativeOfCasesEntity.class);
        return submitImport(ncpCumulativeOfCasesEntitys,2);
    }

    @SysLog("导入临床病例")
    @RequiresPermissions("ncp:cumulativeofcases:import")
    @PostMapping("/importClinic")
    public RestResponse importClinic(MultipartFile file,boolean updateSupport) throws Exception{
        List<NcpCumulativeOfCasesClinic> ncpCumulativeOfCasesClinics=ExcelUtil.importExcel(file,1,1, NcpCumulativeOfCasesClinic.class);
        if(CollUtil.isEmpty(ncpCumulativeOfCasesClinics))
            return RestResponse.error("导入失败！");
        List<NcpCumulativeOfCasesEntity> ncpCumulativeOfCasesEntitys= BeanCopier.copy(ncpCumulativeOfCasesClinics,NcpCumulativeOfCasesEntity.class);
        return submitImport(ncpCumulativeOfCasesEntitys,3);
    }

    @SysLog("导出汇总表")
    @RequiresPermissions("ncp:cumulativeofcases:import")
    @GetMapping("/exportCumulative")
    public void exportCumulative(@RequestParam Map<String, Object> params, HttpServletResponse response)
    {
        List<NcpCumulativeOfCasesEntity> nowList=ncpCumulativeOfCasesService.queryAllToday(params);
        List<NcpCumulativeOfCasesEntity> yesterdayList=ncpCumulativeOfCasesService.queryAllYesterday(params);
        List<NcpCumulativeOfCasesEntity> existList=new ArrayList<NcpCumulativeOfCasesEntity>();
        nowList.forEach(now->yesterdayList.forEach(yesterday->{
            if(now.getIdNumber().equals(yesterday.getIdNumber())&&now.getCasesClassification()==yesterday.getCasesClassification()){
                existList.add(now);
            }
        }));
        existList.forEach(exist->{
            nowList.remove(exist);
        });
        if(CollUtil.isEmpty(nowList)){

        }else{
            Workbook workbook = ExcelUtil.exportCumulative(nowList);
            String fileName = nowList.get(0).getStreet()+new SimpleDateFormat("yyyyMMdd").format(new Date())+"汇总上报表格（新增疑似-新增确诊-新增临床）.xlsx";
            ExcelUtil.downLoadExcel(fileName,response,workbook);
        }

    }

    public RestResponse submitImport(List<NcpCumulativeOfCasesEntity> ncpCumulativeOfCasesEntitys,int casesClassification){
        List<NcpCumulativeOfCasesEntity> nowNcpCumulativeOfCasesEntitys=ncpCumulativeOfCasesService.queryAllToday(null);
        Boolean success=false;
        ncpCumulativeOfCasesEntitys.forEach(ncpCumulativeOfCasesEntity -> {
            ncpCumulativeOfCasesEntity.setCasesClassification(casesClassification);
            setProperties(ncpCumulativeOfCasesEntity);
        });
        if(CollUtil.isEmpty(nowNcpCumulativeOfCasesEntitys)){
            success=ncpCumulativeOfCasesService.saveOrUpdateBatch(ncpCumulativeOfCasesEntitys);
        }else{
            nowNcpCumulativeOfCasesEntitys.forEach(now->ncpCumulativeOfCasesEntitys.forEach(ncpCumulativeOfCasesEntity->{
                        if(now.getIdNumber().equals(ncpCumulativeOfCasesEntity.getIdNumber())){
                            ncpCumulativeOfCasesEntity.setPersId(now.getPersId());
                        }
                    }
            ));
            success=ncpCumulativeOfCasesService.saveOrUpdateBatch(ncpCumulativeOfCasesEntitys);
        }
        if(success)
            return RestResponse.success("导入成功！");
        else
            return RestResponse.error("导入失败！");
    }
    public NcpCumulativeOfCasesEntity setProperties(NcpCumulativeOfCasesEntity ncpCumulativeOfCasesEntity)
    {
        ncpCumulativeOfCasesEntity.setIdNumber(StringUtils.clearSymbol(ncpCumulativeOfCasesEntity.getIdNumber()));
        String ageStr=ncpCumulativeOfCasesEntity.getAgeString();
        if(StringUtils.isBlank(ageStr)){
            ncpCumulativeOfCasesEntity.setAge(IdcardUtils.getAgeByIdCard(ageStr));
        }else{
            ncpCumulativeOfCasesEntity.setAge(StringUtils.getNumber(ageStr));
        }

        String sex=IdcardUtils.getGenderByIdCard(ncpCumulativeOfCasesEntity.getIdNumber());
        if(sex.equals("M")){
            ncpCumulativeOfCasesEntity.setSex(1);
        }else{
            ncpCumulativeOfCasesEntity.setSex(2);
        }
        ncpCumulativeOfCasesEntity.setCreateUserId(getUser().getUserId());
        ncpCumulativeOfCasesEntity.setCreateUserOrgNo(getUser().getOrgNo());
        ncpCumulativeOfCasesEntity.setCreateTime(new Date());
        return ncpCumulativeOfCasesEntity;
    }

    /**
     * 修改
     *
     * @param ncpCumulativeOfCases ncpCumulativeOfCases
     * @return RestResponse
     */
    @SysLog("修改")
    @RequestMapping("/update")
    @RequiresPermissions("ncp:cumulativeofcases:update")
    public RestResponse update(@RequestBody NcpCumulativeOfCasesEntity ncpCumulativeOfCases) {

        ncpCumulativeOfCasesService.update(ncpCumulativeOfCases);

        return RestResponse.success();
    }

    /**
     * 根据主键删除
     *
     * @param persIds persIds
     * @return RestResponse
     */
    @SysLog("删除")
    @RequestMapping("/delete")
    @RequiresPermissions("ncp:cumulativeofcases:delete")
    public RestResponse delete(@RequestBody String[] persIds) {
        ncpCumulativeOfCasesService.deleteBatch(persIds);

        return RestResponse.success();
    }
}
